package com.liwalo.dao;

// Generated 24-ago-2013 14:46:30 by Hibernate Tools 3.6.0

import static org.hibernate.criterion.Example.create;

import java.util.List;

import javax.naming.InitialContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.liwalo.entities.TipoPago;
import com.liwalo.utils.HibernateUtil;

/**
 * Home object for domain model class TipoPago.
 * @see com.liwalo.dao.TipoPago
 * @author Hibernate Tools
 */
public class TipoPagoDAO {

	private static final Log log = LogFactory.getLog(TipoPagoDAO.class);

	private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

	protected SessionFactory getSessionFactory() {
		try {
			return (SessionFactory) new InitialContext()
					.lookup("SessionFactory");
		} catch (Exception e) {
			log.error("Could not locate SessionFactory in JNDI", e);
			throw new IllegalStateException(
					"Could not locate SessionFactory in JNDI");
		}
	}

	public void persist(TipoPago transientInstance) {
		log.debug("persisting TipoPago instance");
		Session session = sessionFactory.getCurrentSession();
        Transaction tx = session.beginTransaction();
		try {
			session.save(transientInstance);
	         tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(TipoPago instance) {
		log.debug("attaching dirty TipoPago instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TipoPago instance) {
		log.debug("attaching clean TipoPago instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(TipoPago persistentInstance) {
		log.debug("deleting TipoPago instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TipoPago merge(TipoPago detachedInstance) {
		log.debug("merging TipoPago instance");
		try {
			TipoPago result = (TipoPago) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public TipoPago findById(java.lang.Integer id) {
		log.debug("getting TipoPago instance with id: " + id);
		try {
			TipoPago instance = (TipoPago) sessionFactory.getCurrentSession()
					.get("com.liwalo.dao.TipoPago", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<TipoPago> findByExample(TipoPago instance) {
		log.debug("finding TipoPago instance by example");
		try {
			List<TipoPago> results = (List<TipoPago>) sessionFactory
					.getCurrentSession()
					.createCriteria("com.liwalo.dao.TipoPago")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}
}
